package com.lg.cms.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.cms.dto.TemplateDTO;
import com.lg.cms.entity.Template;
import com.lg.cms.entity.TemplateType;
import com.lg.cms.entity.TemplateTypePath;
import com.lg.cms.mapper.TemplateMapper;
import com.lg.cms.mapper.TemplateTypeMapper;
import com.lg.cms.mapper.TemplateTypePathMapper;
import com.lg.cms.service.TemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 模板表 服务实现类
 * </p>
 *
 * @author lg
 * @since 2020-09-21
 */
@Service
@Slf4j
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {

    @Autowired
    private TemplateMapper templateMapper;
    @Autowired
    private TemplateTypeMapper templateTypeMapper;
    @Autowired
    private TemplateTypePathMapper templateTypePathMapper;

    @Value("${template.path}")
    String templatePath;
    @Value("${template.uri}")
    String templateUri;

    /**
     * 初始化默认模板
     */
    @PostConstruct
    @Override
    public void initDefaultTemplate() {
        loadDefaultTemplate();
        loadDefaultTemplateFile();
    }

    /**
     * 获取使用模板全信息
     * @return
     */
    @Override
    public TemplateDTO getCurrentTemplate() {
        TemplateDTO dto = new TemplateDTO();
        Map<String, Integer> tempMap = new HashMap<>();
        templateTypeMapper.selectList(null).forEach(v -> tempMap.put(v.getAlias(), v.getId()));
        Template template = templateMapper.selectOne(new QueryWrapper<Template>().eq("`use`", true));
        List<TemplateTypePath> templateTypePathList =
                templateTypePathMapper.selectList
                (new QueryWrapper<TemplateTypePath>().eq("template_id", template.getId()));
        dto.setTemplateId(template.getId());
        dto.setTemplateName(template.getName());
        dto.setTemplateAlias(template.getAlias());
        dto.setBasePath(template.getPath());
        for(TemplateTypePath p: templateTypePathList){
            if(tempMap.get("index").equals(p.getTemplateTypeId())){
                dto.setIndexPath(p.getPath());
            }else if(tempMap.get("list").equals(p.getTemplateTypeId())){
                dto.setListPath(p.getPath());
            }else if(tempMap.get("select").equals(p.getTemplateTypeId())){
                dto.setSelectPath(p.getPath());
            }else if(tempMap.get("info-article").equals(p.getTemplateTypeId())){
                dto.setInfoArticlePath(p.getPath());
            }else if(tempMap.get("info-image").equals(p.getTemplateTypeId())){
                dto.setInfoImagePath(p.getPath());
            }
            else if(tempMap.get("info-video").equals(p.getTemplateTypeId())){
                dto.setInfoVideoPath(p.getPath());
            }else if(tempMap.get("tags-list").equals(p.getTemplateTypeId())){
                dto.setTagsListPath(p.getPath());
            }else if(tempMap.get("image-list").equals(p.getTemplateTypeId())){
                dto.setImageListPath(p.getPath());
            }else if(tempMap.get("video-list").equals(p.getTemplateTypeId())){
                dto.setVideoListPath(p.getPath());
            }
        }
        return dto;
    }

    /**
     * 复制当前项目下默认模板到templatePATH
     */
    private void loadDefaultTemplateFile(){
//        log.info("进入模板初始化");
        String myTemplatePath = templatePath.substring(5);
        String basePath = System.getProperty("user.dir") + "/default";
//        log.info("basePath: " + basePath);
//        log.info("tempPath: " + templatePath);
        FileUtil.copy(basePath, myTemplatePath, true);
    }

    /**
     * 初始化数据
     */
    private void loadDefaultTemplate(){
        Template template = templateMapper.selectOne(new QueryWrapper<Template>().eq("alias", "default"));
        Map<String, Integer> templateTypeMap = new HashMap<>();
        templateTypeMapper.selectList(null).forEach(v -> templateTypeMap.put(v.getAlias(), v.getId()));
        if(template == null){
            template = new Template();
            template.setAlias("default");
            template.setName("默认模板");
            template.setDescription("默认模板");
            template.setPath(templateUri.substring(0, templateUri.lastIndexOf("/")) + "/default/");
            template.setUse(true);
            int result = templateMapper.insert(template);
            if(result != 1){
                log.error("初始化模板失败");
                System.exit(0);
            }
        }
//        List<TemplateTypePath> templateTypePathList =  templateTypePathMapper.selectList
//                (new QueryWrapper<TemplateTypePath>().eq("template_id", template.getId()));
//        for(TemplateTypePath t: templateTypePathList){
//
//        }
        TemplateTypePath tIndex =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("index")));
        if(tIndex == null){
            tIndex = new TemplateTypePath();
            tIndex.setTemplateId(template.getId());
            tIndex.setTemplateTypeId(templateTypeMap.get("index"));
            tIndex.setPath("/default/index");
            templateTypePathMapper.insert(tIndex);
        }
        TemplateTypePath tList =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("list")));
        if(tList == null){
            tList = new TemplateTypePath();
            tList.setTemplateId(template.getId());
            tList.setTemplateTypeId(templateTypeMap.get("list"));
            tList.setPath("/default/list");
            templateTypePathMapper.insert(tList);
        }
        TemplateTypePath tSelect =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("select")));
        if(tSelect == null){
            tSelect = new TemplateTypePath();
            tSelect.setTemplateId(template.getId());
            tSelect.setTemplateTypeId(templateTypeMap.get("select"));
            tSelect.setPath("/default/select");
            templateTypePathMapper.insert(tSelect);
        }
        TemplateTypePath tInfoArticle =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("info-article")));
        if(tInfoArticle == null){
            tInfoArticle = new TemplateTypePath();
            tInfoArticle.setTemplateId(template.getId());
            tInfoArticle.setTemplateTypeId(templateTypeMap.get("info-article"));
            tInfoArticle.setPath("/default/info-article");
            templateTypePathMapper.insert(tInfoArticle);
        }
        TemplateTypePath tInfoImage =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("info-image")));
        if(tInfoImage == null){
            tInfoImage = new TemplateTypePath();
            tInfoImage.setTemplateId(template.getId());
            tInfoImage.setTemplateTypeId(templateTypeMap.get("info-image"));
            tInfoImage.setPath("/default/info-image");
            templateTypePathMapper.insert(tInfoImage);
        }
        TemplateTypePath tInfoVideo =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("info-video")));
        if(tInfoVideo == null){
            tInfoVideo = new TemplateTypePath();
            tInfoVideo.setTemplateId(template.getId());
            tInfoVideo.setTemplateTypeId(templateTypeMap.get("info-video"));
            tInfoVideo.setPath("/default/info-video");
            templateTypePathMapper.insert(tInfoVideo);
        }
        TemplateTypePath tTagsList =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("tags-list")));
        if(tTagsList == null){
            tTagsList = new TemplateTypePath();
            tTagsList.setTemplateId(template.getId());
            tTagsList.setTemplateTypeId(templateTypeMap.get("tags-list"));
            tTagsList.setPath("/default/tags_list");
            templateTypePathMapper.insert(tTagsList);
        }

        TemplateTypePath tImageList =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("image-list")));
        if(tImageList == null){
            tImageList = new TemplateTypePath();
            tImageList.setTemplateId(template.getId());
            tImageList.setTemplateTypeId(templateTypeMap.get("image-list"));
            tImageList.setPath("/default/image_list");
            templateTypePathMapper.insert(tImageList);
        }

        TemplateTypePath tVideoList =
                templateTypePathMapper.selectOne
                        (new QueryWrapper<TemplateTypePath>()
                                .eq("template_id", template.getId())
                                .eq("template_type_id", templateTypeMap.get("video-list")));
        if(tVideoList == null){
            tVideoList = new TemplateTypePath();
            tVideoList.setTemplateId(template.getId());
            tVideoList.setTemplateTypeId(templateTypeMap.get("video-list"));
            tVideoList.setPath("/default/video_list");
            templateTypePathMapper.insert(tVideoList);
        }
    }
}
